package com.example.administrator.yuejian2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.bean.TrainOrderBean;
import com.example.administrator.yuejian2.other.LeftSlideView;
import com.example.administrator.yuejian2.other.Utils;
import java.util.List;


public class MyTrainOrderAdapter extends RecyclerView.Adapter<MyTrainOrderAdapter.MyViewHolder> implements LeftSlideView.IonSlidingButtonListener {

    private Context context;
    private LayoutInflater layoutInflater;
    private List<TrainOrderBean> list;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    private IonSlidingViewClickListener mISetBtnClickListener;
    private LeftSlideView mMenu = null;
    public static String id;
    public MyTrainOrderAdapter(Context context) {

        this.context = context;
        mIDeleteBtnClickListener = (IonSlidingViewClickListener) context;
        mISetBtnClickListener = (IonSlidingViewClickListener) context;
        layoutInflater = LayoutInflater.from(context);

    }

    public void getMyTrainOrderListData(List<TrainOrderBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {

        //获取自定义View的布局（加载item布局）
        View view = LayoutInflater.from(context).inflate(R.layout.my_trainorder_item, arg0, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        final TrainOrderBean bean = list.get(position);
        viewHolder.my_train_order_text.setText(bean.getTrain_name());
        viewHolder.my_train_order_time.setText(bean.getTrainorder_time());

        if (bean.getTrainorder_state().equals("1"))
        {
            viewHolder.my_train_order_state.setText("审核已通过");
        }
        else{
            viewHolder.my_train_order_state.setText("审核未通过");
        }
        //设置内容布局的宽为屏幕宽度
        viewHolder.layout_content.getLayoutParams().width = Utils.getScreenWidth(context);
        //item正文点击事件
        viewHolder.layout_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //判断是否有删除菜单打开
                if (menuIsOpen()) {
                    closeMenu();//关闭菜单
                } else {
                    id= bean.getTrainorder_id();
                    int n = holder.getLayoutPosition();
                    mIDeleteBtnClickListener.onItemClick(v, n);
                }
            }
        });

        //左滑设置点击事件
        holder.btn_Set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                id= bean.getTrainorder_id();
                int n = holder.getLayoutPosition();
                mISetBtnClickListener.onSetBtnCilck(view, n);
            }
        });

        //左滑删除点击事件
        holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int n = holder.getLayoutPosition();
                mIDeleteBtnClickListener.onDeleteBtnCilck(view, n);

            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }

    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView btn_Set;
        public TextView btn_Delete;
        public TextView my_train_order_text,my_train_order_time,my_train_order_state;
        public ViewGroup layout_content;

        public MyViewHolder(View itemView) {
            super(itemView);

                btn_Set=itemView.findViewById(R.id.btn_Set);
            btn_Delete = (TextView) itemView.findViewById(R.id.tv_delete);
            my_train_order_text = (TextView) itemView.findViewById(R.id.my_train_order_text);
            layout_content = (ViewGroup) itemView.findViewById(R.id.layout_content);
            my_train_order_state=itemView.findViewById(R.id.my_train_order_state);
            my_train_order_time=itemView.findViewById(R.id.my_train_order_time);
            ((LeftSlideView) itemView).setSlidingButtonListener(MyTrainOrderAdapter.this);
        }
    }


    /**
     * 删除item
     *
     * @param position
     */
    public void removeData(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }


    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (LeftSlideView) view;
    }


    /**
     * 滑动或者点击了Item监听
     *
     * @param leftSlideView
     */
    @Override
    public void onDownOrMove(LeftSlideView leftSlideView) {
        if (menuIsOpen()) {
            if (mMenu != leftSlideView) {
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * 判断菜单是否打开
     *
     * @return
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }


    /**
     * 注册接口的方法：点击事件。在Mactivity.java实现这些方法。
     */
    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);//点击item正文

        void onDeleteBtnCilck(View view, int position);//点击“删除”

        void onSetBtnCilck(View view, int position);//点击“设置”
    }
}
